import 'package:flutter/material.dart';
import '../widget/imagePage.dart';

class PageViewSwiper extends StatefulWidget {
  const PageViewSwiper({super.key});

  @override
  State<PageViewSwiper> createState() => _PageViewSwiperState();
}

class _PageViewSwiperState extends State<PageViewSwiper> {
  int _currentIndex = 0;
  List<Widget> _list = [];
  @override
  void initState() {
    super.initState();
    _list = [
      ImagePage(imageUrl: 'https://www.itying.com/images/flutter/1.png'),
      ImagePage(imageUrl: 'https://www.itying.com/images/flutter/2.png'),
      ImagePage(imageUrl: 'https://www.itying.com/images/flutter/3.png'),
    ];
  }

  @override
  //无限轮播
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //     appBar: AppBar(title: Text('PageView演示')),
  //     body: SizedBox(
  //       height: 200,
  //       child: PageView.builder(
  //         itemCount: 1000,
  //         itemBuilder:(context,index) {
  //           return _list[index % _list.length];
  //         } ,
  //       ),
  //     ),
  //   );
  // }
  //有操作点的轮播图
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('PageView演示')),
      body: Stack(
        children: [
          SizedBox(
            height: 200,
            child: PageView.builder(
              onPageChanged: (index) {
                setState(() {
                  _currentIndex = index % _list.length;
                });
              },
              itemCount: 1000,
              itemBuilder: (context, index) {
                return _list[index % _list.length];
              },
            ),
          ),
          Positioned(
            left: 0,
            right: 0, //设置left right0 宽度占满屏幕
            bottom: 2,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(_list.length, (index) {
                return Container(
                  margin: EdgeInsets.all(5),
                  width: 10,
                  height: 10,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: _currentIndex == index
                        ? Colors.blue
                        : Colors.grey,
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }
}
